An abstract class in Java is a class that cannot be instantiated on its own and may contain abstract methods, which are methods without an implementation, meant to be subclassed and implemented.
Synchronization in multithreading ensures that only one thread can access a shared resource at a time, preventing data corruption and race conditions by using locks and monitors.
Garbage collection in Java automatically deallocates memory occupied by objects that are no longer referenced, preventing memory leaks and managing memory efficiently. It is used periodically by the JVM.
A constructor is a special type of method used for initializing objects, invoked when an object is created. Methods perform specific tasks and can be called multiple times after object initialization.
An interface defines a contract with abstract methods that implementing classes must fulfill, allowing multiple inheritance. An abstract class can have both abstract and concrete methods, providing partial implementation, but allows only single inheritance.
In Java, threads can be used in two main ways:
Thread
Class: Create a subclass of Thread
and override the run
method.Runnable
Interface: Implement the Runnable
interface and pass an instance of the class to a Thread
object, then call start
.Both methods involve defining the run
method to specify the code that runs in the new thread.
An Iterator in Java is an interface that provides methods (hasNext(), next(), and remove()) to sequentially access and modify elements in a collection safely and efficiently.
In Java, static is a keyword used to indicate that a member (variable, method, block, or nested class) belongs to the class itself rather than to instances of the class. Static members can be accessed without creating an instance of the class.
A final class can't be extended ie., final class may not be subclassed. A final method can't be overridden when its class is inherited. You can't change value of a final variable (is a constant).
The program compiles properly but at runtime it will give "main() method not public." message.
If the static modifier is removed from the signature of the main() method, the Java program will not run because the JVM needs a static main method as the entry point to start execution without requiring an instance of the class.
Program compiles and runs properly.
If you do not provide the String[]
argument to the main
method, the JVM will not recognize it as the entry point, leading to a runtime error with the message "Main method not found in class."
The first argument of the String[] array in the main() method is the first command-line argument passed to the Java program. If no arguments are passed, the array is empty.
You can prove that the array is not null but empty using the following line of code in Java:
System.out.println(array != null && array.length == 0);
To run Java programs, you need to set the JAVA_HOME
variable to the JDK installation directory and add %JAVA_HOME%\bin
to the PATH
variable.
Yes, an application can have multiple classes with a main()
method. However, only one main()
method acts as the entry point for the program, which is specified when running the application.
No, you cannot have multiple main()
methods with the same signature in the same class. Only one main()
method can serve as the entry point for the program.
No, you don't need to import the java.lang
package explicitly. It is automatically imported by the compiler in every Java program because it contains fundamental classes and interfaces like String
, Object
, and System
that are used by default
Checked exceptions are exceptions that are checked at compile-time, meaning the compiler forces you to handle them using a try-catch block or declare them in the method signature using the throws keyword. Examples include IOException and SQLException. Unchecked exceptions are exceptions that are not checked at compile-time. They occur at runtime and are subclasses of RuntimeException. Examples include NullPointerException and ArrayIndexOutOfBoundsException.
Overriding in Java refers to the process of providing a new implementation for a method inherited from a superclass in a subclass. The method signature in the subclass must match that of the superclass.
The default value will be null unless we define it explicitly.
No. A top level class cannot be private or protected. It can have either "public" or no modifier. If it does not have a modifier it is supposed to have a default access. If a top level class is declared as private the compiler will complain that the "modifier private is not allowed here". This means that a top level class can not be private. Same is the case with protected.
In Java the arguments are always passed by value.
In Java, primitive data types are passed by value. This means that when you pass a primitive type to a method, a copy of the value is passed, not the actual variable itself.
In Java, objects are passed by value. When you pass an object to a method, you're passing the reference to the object, not the object itself.
Serialization in Java refers to the process of converting an object into a stream of bytes so that it can be stored in memory, a file, or sent over the network. This allows the object's state to be saved and later reconstructed.
To serialize an object to a file in Java, you can follow these steps:
ObjectOutputStream
by wrapping a FileOutputStream
with the file path where you want to store the serialized object.writeObject()
method of ObjectOutputStream
to write the object to the file.ObjectOutputStream
to release system resources.In Java, the Serializable
interface is a marker interface, meaning it does not have any methods to implement. Simply implementing it enables the serialization mechanism for the class.
The Externalizable
interface in Java is an extension of the Serializable
interface. It provides control over the serialization process by allowing classes to implement custom serialization and deserialization logic through the writeExternal()
and readExternal()
methods.
The serialization mechanism generates an object graph for serialization. Thus it determines whether the included object references are serializable or not. This is a recursive process. Thus when an object is serialized, all the included objects are also serialized alongwith the original obect.
During serialization in Java, static fields are not serialized. They are not part of the state of an object, as they belong to the class itself rather than to individual instances.
Wrapper classes in Java are classes that encapsulate primitive data types, allowing them to be treated as objects. They provide utility methods and are used for conversion between primitive types and objects.
rapper classes are needed in Java for several reasons:
To work with collections: Collections in Java (like ArrayList and HashMap) can only store objects, so wrapper classes allow primitive types to be stored in collections.
To provide utility methods: Wrapper classes provide methods for converting between primitive types and objects, parsing strings, and performing other operations.
To facilitate nullability: Wrapper classes allow null values to be assigned to variables representing primitive types.
To work with generics: Generics in Java do not support primitive types, so wrapper classes are used instead.
Checked exception are those which the Java compiler forces you to catch.
Runtime exceptions are those exceptions that are thrown at runtime because of either wrong input data or because of wrong business logic etc. These are not checked by the compiler at compile time.
In Java, errors and exceptions are both types of Throwable, but they serve different purposes:
Errors: Errors represent serious, unrecoverable problems that are typically caused by the environment in which the Java program is running. Examples include OutOfMemoryError and StackOverflowError.
Exceptions: Exceptions represent conditions that can be handled and recovered from within the program. They are often caused by programmatic errors or unexpected conditions. Examples include NullPointerException and FileNotFoundException.
To create a custom exception in Java, follow these steps:
Here's an example:
public class CustomException extends Exception { public CustomException() { super(); } public CustomException(String message) { super(message); } public CustomException(String message, Throwable cause) { super(message, cause); } }
ou can then throw this custom exception in your code using the throw keyword.
Yes, the finally block will still execute even if a return statement is encountered inside the try block.
In Java, the Observer pattern is implemented using the Observer
interface and the Observable
class.
Observable: This class represents the subject being observed. It maintains a list of observers and notifies them of any changes using the notifyObservers()
method.
Observer: This interface represents the observer that wants to be notified of changes in the observable. It defines the update()
method, which is called by the observable when it changes.
To use them:
Observable
and implement the logic that changes the state of the object.Observer
in the classes that need to be notified of changes.addObserver()
method.setChanged()
followed by notifyObservers()
to notify all registered observers.